/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Unless required by applicable law or
 * agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package org.rhok.pdx.ncn.application;

import org.rhok.pdx.ncn.R;
import org.rhok.pdx.ncn.listeners.TrackingLocationListener;
import org.rhok.pdx.ncn.listeners.TrackingPhoneStateListener;
import org.rhok.pdx.ncn.listeners.WifiSignalTracker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;

/**
 * Shows the raw data fields containing the current GPS coordinates and signal strength
 * @author afitzgibbon
 */
public class DataMenu extends Activity {
    
    private final ApplicationContext context = new ApplicationContext(this);
    private LocationListener locListener;
    private PhoneStateListener psListener;
    private LocationManager locMgr;
    private TelephonyManager teleMgr;
    private WifiManager wifiMgr;
    private WifiSignalTracker wifiTracker;
    
    /**
     * Run on creation of the application. Initializes the context, sets the layout, and begins
     * tracking data. This method should never be called directly.
     * @param savedInstanceState Information from saved instance of application, passed in by system
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data_layout);
        
        initDataTracking();
    }
    
    /**
     * Return to the map view.
     *
     * @param view
     */
    public void returnToMap(View view) {
        Intent intent = new Intent(this, MapMenu.class);
        startActivity(intent);
        finish();
    }
    
    /**
     * Initializes data collection procedures. Starts collections location, cell signal, and wifi
     * signal information. Each listener / data collector stores said data in the application
     * SQLite database.
     */
    private void initDataTracking() {
        locListener = new TrackingLocationListener(context);
        psListener = new TrackingPhoneStateListener(context);

        locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10,
                locListener);

        teleMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        teleMgr.listen(psListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTH);

        wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiTracker = new WifiSignalTracker(context, wifiMgr);
        wifiTracker.start();
    }
}